// Figure 7: Specification Chart by Position //

clear all
cap log close

global path ""
use "$path/Main.dta", clear

cap program drop specchart
program specchart
syntax varlist, [replace] spec(string)
	* save current data
	tempfile temp
	save "`temp'",replace
	*dataset to store estimates
	if "`replace'"!=""{
			clear
			gen beta=.
			gen se=.
			gen spec_id=.
			gen u95=.
			gen u90=.
			gen l95=.
			gen l90=.
			save "estimates.dta",replace
	}	
	else{
		* load dataset
		use "estimates.dta",clear
	}
	* add observation
	local obs=_N+1
	set obs `obs'
	replace spec_id=`obs' if _n==`obs'
	* store estimates
	replace beta =_b[`varlist'] if  spec_id==`obs'
	replace se=_se[`varlist']   if  spec_id==`obs'
	replace u95=beta+invt(e(df_r),0.975)*se if  spec_id==`obs'
	replace u90=beta+invt(e(df_r),0.95)*se if  spec_id==`obs'
	replace l95=beta-invt(e(df_r),0.975)*se  if  spec_id==`obs'
	replace l90=beta-invt(e(df_r),0.95)*se  if  spec_id==`obs'
	* store specification
	foreach s in `spec'{
		cap gen `s'=1 			if  spec_id==`obs'
		cap replace `s'=1 		 if  spec_id==`obs'
	}
		save "estimates.dta",replace
	* restore dataset
	use `temp',clear
end

capture reg log_pc_x_totexp win_M win_VM win_G win_VG win_CONG
specchart win_M,spec(FE_no FCN_no) replace

local FCN_no ""
local FCN_linear "mv_*_1 dmv_*_1"
local FCN_quadratic "mv_*_1 dmv_*_1 mv_*_2 dmv_*_2"

foreach r in no linear quadratic {

xtset idcode

capture reg log_pc_x_totexp win_M win_VM win_G win_VG win_CONG `FCN_`r'', robust cluster(idcode_prov)
foreach p in M VM G VG CONG {
specchart  win_`p',spec(POS_`p' FCN_`r') 
}

capture reg log_pc_x_totexp win_M win_VM win_G win_VG win_CONG `FCN_`r'' i.year, robust cluster(idcode_prov)
foreach p in M VM G VG CONG {
specchart  win_`p',spec(POS_`p' FE_year FCN_`r') 
}

capture xi: xtreg log_pc_x_totexp win_M win_VM win_G win_VG win_CONG `FCN_`r'', fe robust cluster(idcode_prov)
foreach p in M VM G VG CONG {
specchart  win_`p',spec(POS_`p' FE_mun FCN_`r') 
}

capture xi: xtreg log_pc_x_totexp win_M win_VM win_G win_VG win_CONG `FCN_`r'' i.year, fe robust cluster(idcode_prov)

foreach p in M VM G VG CONG {
specchart  win_`p',spec(POS_`p' FE_year FE_mun FCN_`r')
}
}

use "estimates.dta",clear
drop if _n == 1
replace spec_id = spec_id - 1

duplicates drop POS_M POS_VM POS_G POS_VG POS_CONG FE_year FE_mun FCN_no FCN_linear FCN_quadratic, force

gsort -POS_CONG -POS_G -POS_VG -POS_M -POS_VM ///
	-FE_mun -FE_year ///
	-FCN_quadratic -FCN_linear -FCN_no, mfirst

gen rank=_n

	local scoff=" "
	local scon=" "
	local ind=-1.117

	foreach var in FE_year FE_mun FCN_no FCN_linear FCN_quadratic {
	   cap gen i_`var'=`ind'
	   local ind=`ind'-0.1
	   
	   local scoff="`scoff' (scatter i_`var' rank,msize(vsmall) mcolor(gs10))" 
	   local scon="`scon' (scatter i_`var' rank if `var'==1,msize(vsmall) mcolor(black))"
	   
	   if "`var'" == "FE_mun" {
	   local ind=`ind'-0.2
	   }
	}

gen zero = 0

tw (rbar u95 l95 rank, fcolor(gs12) lcolor(gs12) lwidth(none)) /// 95% CI
   (rbar u90 l90 rank, fcolor(gs6) lcolor(gs16) lwidth(none)) /// 90% CI
   (scatter beta rank, mcolor(black) msymbol(d) msize(*0.5)) ///  point estimates
   (rbar u95 l95 rank if POS_G == 1 | POS_M == 1, fcolor(gs14) lcolor(gs14) lwidth(none)) /// 95% CI
   (rbar u90 l90 rank if POS_G == 1 | POS_M == 1, fcolor(gs11) lcolor(gs16) lwidth(none)) /// 90% CI
   (scatter beta rank if POS_G == 1 | POS_M == 1, mcolor(gs3) msymbol(d) msize(*0.5)) ///  point estimates
   (line zero rank, lcolor(red) lpattern(shortdash)) /// zero line
   `scoff' `scon' /// indicators for spec
   , legend(order(3 "Point Estimate" 1 "95% CI" 5 "90% CI" 2 "90% CI") region(lcolor(white)) ///
	pos(6) ring(1) rows(1) size(vsmall) symysize(small) symxsize(small)) ///
   xtitle(" ") ytitle(" ") title("Dependent Variable: Municipal Spending", size(medsmall)) ///
   yscale(noline) xscale(noline) ylab(-0.5(0.25)0.5,noticks nogrid angle(horizontal) labsize(vsmall)) xlab("", noticks)  ///
   graphregion (fcolor(white) lcolor(white)) plotregion(fcolor(white) lcolor(white)) ///
   text(-0.8 6.5 "Vice Mayor", size(vsmall)) text(-0.8 18.5 "Mayor", size(vsmall)) text(-0.8 30.5 "Vice Governor", size(vsmall)) ///
   text(-0.8 42.5 "Governor", size(vsmall)) text(-0.8 54.5 "Congressperson", size(vsmall)) ///
   text(-1.925 11.25 "{&bullet}", size(medlarge)) text(-1.925 12.25 "Included in the Regression", placement(e) size(vsmall)) ///
   text(-1.925 31 "{&bullet}", color(gs10) size(medlarge)) text(-1.925 32 "Excluded from the Regression", placement(e) size(vsmall))

gr_edit .yaxis1.add_ticks -0.8 `"Dynastic Linkage"', custom tickset(major) editstyle(tickstyle(textstyle(size(small))) )

gr_edit .yaxis1.add_ticks -1.0 `"Fixed Effects"', custom tickset(major) editstyle(tickstyle(textstyle(size(small))) )
gr_edit .yaxis1.add_ticks -1.1 `"Year"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -1.2 `"Municipality"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )

gr_edit .yaxis1.add_ticks -1.4 `"Function"', custom tickset(major) editstyle(tickstyle(textstyle(size(small))) )
gr_edit .yaxis1.add_ticks -1.5 `"None"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -1.6 `"Linear"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -1.7 `"Quadratic"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )

gr_edit .yaxis1.add_ticks 0.7 `"Coefficient"', custom tickset(major) editstyle(tickstyle(textstyle(size(small))) )
